<?php
session_start();
if(!empty($_SESSION["userType"]))
  {
	$userType = $_SESSION["userType"];
  }
	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
	$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
	$NRIC = isset($_POST['NRIC']) ? mysql_real_escape_string($_POST['NRIC']) : '';
        $CustomerName = isset($_POST['CustomerName']) ? mysql_real_escape_string($_POST['CustomerName']) : '';
        
	$Date = isset($_POST['Date']) ? mysql_real_escape_string($_POST['Date']) : '';
	$CentreLocation = isset($_POST['CentreLocation']) ? mysql_real_escape_string($_POST['CentreLocation']) : '';
	$ActivityId = isset($_POST['ActivityId']) ? mysql_real_escape_string($_POST['ActivityId']) : '';
	if($CentreLocation==0)
	{
		$CentreLocation="";
	}
	if($ActivityId==0)
	{
		$ActivityId="";
	}
	$trimmed = trim($Date);
	$offset = ($page-1)*$rows;
	$result = array();

	include 'conn.php';
	if($userType=="System Admin")
	{
					$rs = mysql_query("SELECT count(*)
			FROM activityrecord ar, centre c, activity a ,client cl
			WHERE ar.ActivityId = a.ActivityId
			AND ar.CentreId = c.CentreId AND ar.NRIC=cl.NRIC
			AND (
			ar.NRIC LIKE  '%$NRIC%'
			AND ar.Date LIKE  '%$trimmed%'
			AND ar.CentreId LIKE  '%$CentreLocation%'
                            AND cl.CustomerName LIKE '%$CustomerName%'
			AND ar.ActivityId LIKE  '%$ActivityId%'
			)");
				$row = mysql_fetch_row($rs);
				$result["total"] = $row[0];
				
				$rs = mysql_query("SELECT ar.ActivityRecordID, cl.CustomerName, a.Category, ar.NRIC, ar.Date, c.CentreName, a.ActivityName, ar.Performance
			FROM activityrecord ar, centre c, activity a,client cl
			WHERE ar.ActivityId = a.ActivityId
			AND ar.CentreId = c.CentreId AND ar.NRIC=cl.NRIC
			AND (
			ar.NRIC LIKE  '%$NRIC%'
			AND ar.Date LIKE  '%$trimmed%'
			AND ar.CentreId LIKE  '%$CentreLocation%'
                            AND cl.CustomerName LIKE '%$CustomerName%'
			AND ar.ActivityId LIKE  '%$ActivityId%'
			)ORDER BY ar.Date DESC limit $offset,$rows");
				
				$items = array();
				while($row = mysql_fetch_object($rs)){
					array_push($items, $row);
				}
				$result["rows"] = $items;

				echo json_encode($result);
	}
	else
	{
	if(!empty($_SESSION["centreName"]))
  {
	$centreName = $_SESSION["centreName"];
  }
		$rs = mysql_query("SELECT count(*)
FROM activityrecord ar, centre c, activity a, client cl
WHERE ar.ActivityId = a.ActivityId
AND ar.CentreId = c.CentreId AND ar.NRIC=cl.NRIC
AND (
ar.NRIC LIKE  '%$NRIC%'
AND ar.Date LIKE  '%$trimmed%'
    AND cl.CustomerName LIKE '%$CustomerName%'
AND ar.CentreId IN (Select CentreId from Centre where CentreName='$centreName')
AND ar.ActivityId LIKE  '%$ActivityId%'
)");
	$row = mysql_fetch_row($rs);
	$result["total"] = $row[0];
	
	$rs = mysql_query("SELECT ar.ActivityRecordID,cl.CustomerName,a.Category, ar.NRIC, ar.Date, c.CentreName, a.ActivityName,ar.Performance
FROM activityrecord ar, centre c, activity a, client cl
WHERE ar.ActivityId = a.ActivityId
AND ar.CentreId = c.CentreId AND ar.NRIC=cl.NRIC
AND (
ar.NRIC LIKE  '%$NRIC%'
AND ar.Date LIKE  '%$trimmed%'
    AND cl.CustomerName LIKE '%$CustomerName%'
AND ar.CentreId IN (Select CentreId from Centre where CentreName='$centreName')
AND ar.ActivityId LIKE  '%$ActivityId%'
)ORDER BY ar.Date DESC limit $offset,$rows");
	
	$items = array();
	while($row = mysql_fetch_object($rs)){
		array_push($items, $row);
	}
	$result["rows"] = $items;

	echo json_encode($result);
	}
	

?>